package com.ruoyi.web.controller.Base;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.Ability_InstitutionInfor;
import com.ruoyi.domain.Ability_UserInfor;
import com.ruoyi.service.Ability_UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    Ability_UserService ability_userService;

    @PostMapping("/addUser")
    public AjaxResult insertUserInfor(@RequestBody Ability_UserInfor userInfor) {
        int result = ability_userService.insertUserInfor(userInfor);
        if (result > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }


    }

    @PostMapping("/delUser")
    public AjaxResult deleteUserInfor(@RequestBody Ability_UserInfor userInfor) {
        int result = ability_userService.deleteUserInfor(userInfor);
        if (result > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败，请检查输入正确或是否存在外键");
        }
    }

    @GetMapping("/delUserById/{id}")
    public AjaxResult deleteUserInforById(@PathVariable("id") Integer id) {
        int result = ability_userService.deleteUserInforById(id);
        if (result > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败，请检查输入正确或是否存在外键");
        }
    }

    @PostMapping("/upUser")
    public AjaxResult updateUserInfor(@RequestBody Ability_UserInfor userInfor) {
        int result = ability_userService.updateUserInfor(userInfor);
        if (result > 0) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.error("更新失败");
        }
    }

    @GetMapping("/list")
    public TableDataInfo selectUserInforAll(@RequestParam(required = false, value = "sername") String name,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, // 添加默认值可选
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        startPage();
        Ability_UserInfor userInfor=new Ability_UserInfor();
        userInfor.setAccount(name);
        List<Ability_UserInfor> list = ability_userService.selectUserInforAll(userInfor);
        return getDataTable(list);
    }


}
